package cn.whdream.datastructure.linkedlist;

import java.util.Stack;

/**
 * 
 * @author admin 链表顺序添加
 */
public class DoubleLinkedListDemo {
	public static void main(String[] args) {
		DoubleLinkedList list = new DoubleLinkedList();
//		list.addHeroNode(new HeroNode2("李逵", "黑旋风", 4));
//		list.addHeroNode(new HeroNode2("宋江", "及时雨", 1));
//		list.addHeroNode(new HeroNode2("卢俊义", "玉麒麟", 2));
//		list.addHeroNode(new HeroNode2("吴用", "智多星", 3));
		list.addHeroNodeByOrderAsc(new HeroNode2("李逵", "黑旋风", 4));
		list.addHeroNodeByOrderAsc(new HeroNode2("宋江", "及时雨", 1));
		list.addHeroNodeByOrderAsc(new HeroNode2("卢俊义", "玉麒麟", 2));
		list.addHeroNodeByOrderAsc(new HeroNode2("吴用", "智多星", 3));
		list.show();
		list.updateHeroNodeByNo(new HeroNode2("诸葛亮", "智多星", 3));
//		list.show();
		list.deleteHeroNodeByNo(3);
//		list.show();
//		list.showLength();
//		System.out.println(list.findLastIndexNode(4));
		System.out.println("翻转列表");
		list.showReverseList();
	}

	static class DoubleLinkedList {
		private final HeroNode2 head = new HeroNode2("", "", 0);

		public void addHeroNode(HeroNode2 node) {
			HeroNode2 temp = head;
			while (true) {
				if (temp.next == null) {
					break;
				}
				temp = temp.next;
			}
			//指定后一个节点
			temp.next = node;
			//指定前一个节点
			node.pre = temp;

		}
		public int showLength() {
			int len = 0;
			if(head.next == null) {
				System.out.println("0");
				return 0;
			}
			HeroNode2 node = head.next;
			while(true) {
				if (node != null) {
					len++;
				}else {
					break;
				}
				node = node.next;
			}
			System.out.println(len);
			return len;
		}
		/**
		 * 
		 * @param head 头结点
		 * @param index 倒数的第几个
		 * @return
		 */
		public HeroNode2 findLastIndexNode(int index) {
			int len = showLength();
			if (len == 0 ||index <= 0|| index > len) {
				return null;  
			}
			int findIndex = len - index;
			HeroNode2 node = head.next;
			int curIndex = -1;
			while(true) {
				if (node != null) {
					curIndex++;
					if(curIndex == findIndex) {
						return node;
					}
				}else {
					break;
				}
				node = node.next;
			}
			return null;
		}
		/**
		 * 升序排序
		 * @param node 结点
		 */
		public void addHeroNodeByOrderAsc(HeroNode2 node) {
			HeroNode2 temp = head;
			//用于提示标签
			boolean flag = false;
			while (true) {
				//头结点之后没有数据，直接插入
				if (temp.next == null) {
					temp.next = node;
					node.pre = temp;
					break;	
				}
				if (temp.next.no == node.no) {
					flag = true;
					break;
				}else if(temp.next.no > node.no){
					//插入的数字小，则放到前面
					//指向temp
					node.pre = temp;
					//next指向temp.next
					node.next = temp.next;
					//temp.next断开前面的内容
					temp.next.pre = node;
					//原有的下一个改为node
					temp.next = node;
					break;
				}else {
					temp = temp.next;
				}
			}
			
			if(flag) {
				System.out.println("该排名已经有英雄，无法插入");
			} 
		}
		public void updateHeroNodeByNo(HeroNode2 h) {
			if(head.next == null) {
				System.out.println("链表为空");
				return;
			}
			HeroNode2 temp = head.next;
			while(true) {
				if(temp == null) {
					break;
				}
				if(temp.no == h.no) {
					temp.name = h.name;
					temp.nickName = h.nickName;
					System.out.println("更新成功:"+temp);
					return;
				}
				temp = temp.next;
			}
			System.out.println("未找到对应英雄");
		}
		public void deleteHeroNodeByNo(int no) {
			HeroNode2 temp = head;
			while(true) {
				if(temp.next == null) {
					break;
				}
				if(temp.next.no == no) {
					HeroNode2 nextNodeChild = temp.next.next;
					HeroNode2 deleteNode = temp.next;
					temp.next = nextNodeChild;
					nextNodeChild.pre = temp;
					System.out.println("删除成功"+deleteNode);
					return;
				}
				temp = temp.next;
			}
			System.out.println("未找到对应英雄");
		}
		
		public void show() {
			HeroNode2 temp = head;
			while (true) {
				if (temp.next == null) {
					break;
				}
				System.out.println(temp.next);
				temp = temp.next;
			}
		}
		public void showReverseList() {
			HeroNode2 temp = head.next;
			if (head.next == null) {
				System.out.println("列表为空");
				return;
			}
			HeroNode2 lastNode;
			while(true) {
				if(temp.next == null) {
					lastNode = temp;
					break;
				}
				temp = temp.next;
			}
			while(true) {
				if(lastNode == head) {
					System.out.println("打印结束");
					break;
				}
				System.out.println(lastNode);
				lastNode = lastNode.pre;
			}
			
		}
		
		
	}

	static class HeroNode2 {
		String name;
		String nickName;
		int no;
		HeroNode2 next;
		HeroNode2 pre;	
		public HeroNode2(String name, String nickName, int no) {
			this.name = name;
			this.nickName = nickName;
			this.no = no;
		}
		@Override
		public String toString() {
			return "HeroNode2 [name=" + name + ", no=" + no + "]";
		}
		
		
	}
}
